package com.twu.biblioteca;

import java.util.*;


public class UserAccountLibrary {

    private Hashtable<String,UserAccount> users;

    private int libraryNo = 1111111;

    public UserAccountLibrary() {

        users =new Hashtable<String, UserAccount>();

        loadAccounts();
    }

    public void loadAccounts() {

        String username;
        for(int i=0 ; i<5 ; i++) {
            username=generateUsername();
            UserAccount uAcc = new UserAccount(username,username);
            users.put(uAcc.getUsername() , uAcc);

        }

    }

    private String generateUsername(){

        String username = Integer.toString(libraryNo);
        username = new StringBuilder(username).insert(3,'-').toString();
        libraryNo++;
        return username;
    }

    public boolean validateUsernamePassword(UserAccount user){

        if(users.containsKey(user.getUsername()))
            if( users.get( user.getUsername() ).getPassword().equals( user.getPassword() ) ) {
                System.out.println("You are logged in ! ");
                return true;
            }

            else{
                System.out.println("Incorrect Password!!");
                return false;
            }


        else {
            System.out.println("Invalid Username!!");
            return false;

        }

    }

}
